//
//  CCondition.m
//  HCHK_iphone
//
//  Created by yilong zhang on 11-12-13.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "CCondition.h"

@implementation CCondition

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

@end

@implementation CDesCounter

@synthesize m_nOffset;
@synthesize m_nLimit;
@synthesize m_nTotal;

-(id)init
{
    self = [super init];
    if (self)
    {
        m_nTotal = 0;
        m_nLimit = 0;
        m_nOffset= 0;
    }
    
    return self;
}

-(void)InitDesWithCout:(int)count Limit:(int)limit
{
    if ((count > 0) && (limit > 0))
    {
        m_nTotal = count;
        m_nOffset= m_nTotal;
        m_nLimit = limit;
        
        m_nOffset = m_nOffset - (limit - 1);
        if (m_nOffset < 0)
        {
            m_nLimit += m_nOffset;
            m_nOffset = 0;
        }
    }
}

-(int)GetDesNextLimit
{
    int nlimit = m_nLimit;
    
    m_nOffset -= 1;
    if (m_nOffset < 0)
    {
        m_nLimit = 0;
        nlimit   = m_nLimit;
    }
    else 
    {
        m_nOffset = m_nOffset - (m_nLimit - 1);
        if (m_nOffset < 0)
        {
            m_nLimit += m_nOffset;
            m_nOffset = 0;
            nlimit    = m_nLimit;
        }
    }
    
    return nlimit;
}

@end

@implementation CAscCounter

@synthesize m_nOffset;
@synthesize m_nLimit;
@synthesize m_nTotal;

-(id)init
{
    self = [super init];
    if (self)
    {
        m_nTotal = 0;
        m_nOffset= 0;
        m_nLimit = 0;
    }
    
    return self;
}

-(void)InitAscWithCount:(int)count Limit:(int)limit
{
    if (count > 0)
    {
        m_nTotal = count;
        m_nLimit = limit;
        m_nOffset= 0;
        
        if ((m_nOffset + (limit - 1)) > m_nTotal)
        {
            m_nLimit = m_nTotal;
        }
    }
}

-(int)GetAscNextLimit:(int)count Cnt:(int)cnt
{
    m_nTotal = count;
    m_nOffset += cnt;
    
    return m_nLimit;
}

-(int)GetAscNextLimit
{
    m_nOffset += m_nLimit;
    
    if ((m_nOffset + (m_nLimit - 1)) > m_nTotal)
    {
        m_nLimit += m_nTotal - (m_nOffset + (m_nLimit - 1));
    }
    
    return m_nLimit;
}

@end
